package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.net.nsd.NsdManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.DiscoverFeed;
import reduce.fz.com.reduce.bean.Feed;
import reduce.fz.com.reduce.bean.Society;
import reduce.fz.com.reduce.bean.Tag;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.DiscoverFragment;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * Created by qingshengzheng on 16/2/16.
 */
public class DiscoverItemAdapter extends RecyclerView.Adapter<DiscoverItemAdapter.BaseItemViewHolder> {
    private DiscoverFeed feed;
    private LayoutInflater layoutInflater;
    private Context context;
    private DiscoverFragment.DiscoverItemListener listener;
    public static final int TYPE_MORE = 4;

    public DiscoverItemAdapter(Context context, DiscoverFragment.DiscoverItemListener listener) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.listener = listener;
    }

    public void initFeed(DiscoverFeed feed) {
        this.feed = feed;
        notifyDataSetChanged();

    }

    @Override
    public BaseItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        BaseItemViewHolder holder = null;

        switch (viewType) {
            case 0:
                view = layoutInflater.inflate(R.layout.view_discover_tag_item, parent, false);
                holder = new TagItemViewHolder(view, listener);
                return holder;
            case 1:
                view = layoutInflater.inflate(R.layout.view_discover_society_item, parent, false);
                holder = new SocietyItemViewHolder(view, listener);
                return holder;
            case 2:
                view = layoutInflater.inflate(R.layout.view_discover_user_item, parent, false);
                holder = new UserItemViewHolder(view, listener);
                return holder;
            case 3:
                view = layoutInflater.inflate(R.layout.view_discover_video_item, parent, false);
                holder = new VideoCardItemViewHolder(view, listener);
                return holder;
            case TYPE_MORE:
                view = layoutInflater.inflate(R.layout.view_discover_more_item, parent, false);
                holder = new DiscoverMoreViewHolder(view, listener);
                return holder;
            default:
                return null;

        }
    }

    @Override
    public void onBindViewHolder(BaseItemViewHolder holder, int position) {
        if (feed.morenum > 0 && position == getItemCount() - 1) {
            ((DiscoverMoreViewHolder) holder).renderView(feed);
            return;
        }
        switch (feed.type) {
            case DiscoverFeed.TYPE_TAG:
                ((TagItemViewHolder) holder).renderView(feed.tags.get(position));
                break;
            case DiscoverFeed.TYPE_SOCIETY:
                ((SocietyItemViewHolder) holder).renderView(feed.societys.get(position));
                break;
            case DiscoverFeed.TYPE_USER:
                ((UserItemViewHolder) holder).renderView(feed.users.get(position));
                break;
            case DiscoverFeed.TYPE_VIDEO:
                ((VideoCardItemViewHolder) holder).renderView(feed.videoCards.get(position));
                break;
            default:
                break;
        }

    }

    public int getItemViewType(int position) {
        if (feed.morenum > 0 && position == (getItemCount() - 1))
            return TYPE_MORE;
        else
            return feed.type;

    }

    @Override
    public int getItemCount() {
        switch (feed.type) {
            case 0:
                return feed.morenum > 0 ? feed.tags.size() + 1 : feed.tags.size();
            case 1:
                return feed.morenum > 0 ? feed.societys.size() + 1 : feed.societys.size();
            case 2:
                return feed.morenum > 0 ? feed.users.size() + 1 : feed.users.size();
            case 3:
                return feed.morenum > 0 ? feed.videoCards.size() + 1 : feed.videoCards.size();
            default:
                return 0;
        }
    }

    public static abstract class BaseItemViewHolder extends RecyclerView.ViewHolder {
        protected DiscoverFragment.DiscoverItemListener listener;
        protected View itemView;

        public BaseItemViewHolder(View itemView, DiscoverFragment.DiscoverItemListener listener) {
            this(itemView);
            this.listener = listener;
            this.itemView = itemView;
        }


        public BaseItemViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        public abstract void initWidget(View itemView);

    }

    public static class UserItemViewHolder extends BaseItemViewHolder {
        private RoundNetworkImageView mavatar;

        public UserItemViewHolder(View itemView, DiscoverFragment.DiscoverItemListener listener) {
            super(itemView, listener);
        }

        @Override
        public void initWidget(View itemView) {
            mavatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
        }


        public void renderView(final User user) {
            mavatar.setImageUrl(user.avatar, HttpUtils.getAvatarImageLoader());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickUser(user);
                }
            });
        }
    }

    public static class TagItemViewHolder extends BaseItemViewHolder {
        private RoundNetworkImageView mItem;

        public TagItemViewHolder(View itemView, DiscoverFragment.DiscoverItemListener listener) {
            super(itemView, listener);
        }


        @Override
        public void initWidget(View itemView) {
            mItem = (RoundNetworkImageView) itemView.findViewById(R.id.niv_item);
        }

        public void renderView(final Tag tag) {
            mItem.setImageUrl(tag.url, HttpUtils.getImageLoader());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickTag(tag);
                }
            });

        }
    }

    public static class SocietyItemViewHolder extends BaseItemViewHolder {
        private NetworkImageView mItem;
        private TextView mName;

        public SocietyItemViewHolder(View itemView, DiscoverFragment.DiscoverItemListener listener) {
            super(itemView, listener);
        }

        public SocietyItemViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void initWidget(View itemView) {
            mItem = (NetworkImageView) itemView.findViewById(R.id.niv_item);
            mName = (TextView) itemView.findViewById(R.id.tv_name);
        }

        public void renderView(final Society society) {
            mItem.setImageUrl(society.thumurl, HttpUtils.getImageLoader());
            //mName.setText(society.sname);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickSociety(society);
                }
            });

        }
    }

    public static class DiscoverMoreViewHolder extends BaseItemViewHolder {
        private TextView mDesc;

        public DiscoverMoreViewHolder(View itemView, DiscoverFragment.DiscoverItemListener listener) {
            super(itemView, listener);
        }

        public DiscoverMoreViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void initWidget(View itemView) {
            mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
        }

        public void renderView(final DiscoverFeed feed) {
            mDesc.setText("查看更多");
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (feed.type) {
                        case DiscoverFeed.TYPE_TAG:
                            listener.clickMoreTag(feed.tagid);
                            break;
                        case DiscoverFeed.TYPE_SOCIETY:
                            listener.clickMoreSociety(feed.tagid);
                            break;
                        case DiscoverFeed.TYPE_USER:
                            listener.clickMoreUser(feed.tagid);
                            break;
                        case DiscoverFeed.TYPE_VIDEO:
                            listener.clickMoreVideoCard(feed.tagid);
                            break;
                    }
                }
            });
        }
    }


    public static class VideoCardItemViewHolder extends BaseItemViewHolder {

        private RoundNetworkImageView mItem;
        private TextView mDesc;

        public VideoCardItemViewHolder(View itemView, DiscoverFragment.DiscoverItemListener listener) {
            super(itemView, listener);
        }

        public VideoCardItemViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void initWidget(View itemView) {
            mItem = (RoundNetworkImageView) itemView.findViewById(R.id.niv_item);
            mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
        }

        public void renderView(final VideoCard card) {
            mItem.setImageUrl(card.video.thumUrl, HttpUtils.getImageLoader());
            // mDesc.setText(card.description);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.clickVideoCard(card);
                }
            });

        }
    }


}
